package com.xspace.android.imcommon.message;

import android.content.Context;
import android.os.RemoteException;

import com.xspace.android.imcommon.db.BaseSMSDBBiz;
import com.xspace.android.imcommon.db.InviteSMSDBBiz;
import com.xspace.android.imcommon.db.LAWSMSDBBiz;
import com.xspace.android.imcommon.db.QACSMSDBBiz;
import com.xspace.android.imcommon.db.ResumeSMSDBBiz;
import com.xspace.android.imcommon.db.SMSDBBiz;
import com.xspace.android.imcommon.db.SPDMSMSDBBiz;
import com.xspace.android.imcommon.interfaces.SMSCallback;
import com.xspace.android.imcommon.interfaces.SmsListAidl;
import com.xspace.android.xafcommon.util.ServiceConstant;

public class SmsManager {
	private InviteSMSDBBiz mInviteSMSDBBiz;
	private LAWSMSDBBiz mLAWSMSDBBiz;
	private QACSMSDBBiz mQACSMSDBBiz;
	private ResumeSMSDBBiz mResumeSMSDBBiz;
	private SPDMSMSDBBiz mSPDMSMSDBBiz;
	private SMSDBBiz mSMSDBBiz;
	private SMSCallback mSMSCallback;

	public SmsManager(Context mContext) {
		this.mInviteSMSDBBiz = new InviteSMSDBBiz(mContext);
		this.mLAWSMSDBBiz = new LAWSMSDBBiz(mContext);
		this.mQACSMSDBBiz = new QACSMSDBBiz(mContext);
		this.mResumeSMSDBBiz = new ResumeSMSDBBiz(mContext);
		this.mSMSDBBiz = new SMSDBBiz(mContext);
		this.mSPDMSMSDBBiz = new SPDMSMSDBBiz(mContext);
	}

	public void initSms(SMSCallback mSMSCallback) {
		this.mSMSCallback = mSMSCallback;
	}

	public SmsListAidl querySmsList(String smsType, String uuid) {
		// TODO Auto-generated method stub
		return new SmsListAidl(getDbClass(smsType).querySMS(uuid));
	}

	public void deleteSmsInfo(String smsType, int id) {
		// TODO Auto-generated method stub
		getDbClass(smsType).deleteSMS(id);
	}

	public void updateSmsRead(String smsType, String userId) {
		// TODO Auto-generated method stub
		getDbClass(smsType).updateSMS(userId);
	}

	public void unReadSmsQury(String jid) {
		unReadBack(ServiceConstant.PLATFORM_QACWEB, mQACSMSDBBiz.queryUnReadSMS(jid));
		unReadBack(ServiceConstant.PLATFORM_LAWWEB, mLAWSMSDBBiz.queryUnReadSMS(jid));
		unReadBack(ServiceConstant.PLATFORM_SQDM, mSPDMSMSDBBiz.queryUnReadSMS(jid));
		unReadBack(ServiceConstant.PLATFORM_NMG_CHAT, mSMSDBBiz.queryUnReadSMS(jid) + mInviteSMSDBBiz.queryUnReadSMS(jid) + mResumeSMSDBBiz.queryUnReadSMS(jid));
	}

	private void unReadBack(String smsType, int unreadSize) {
		try {
			mSMSCallback.onToUnRead(unreadSize, smsType);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private BaseSMSDBBiz getDbClass(String type) {
		if (ServiceConstant.PLATFORM_QACWEB.equals(type)) {// 农机院
			return mQACSMSDBBiz;
		} else if (ServiceConstant.PLATFORM_LAWWEB.equals(type)) {// 法律援助
			return mLAWSMSDBBiz;
		} else if (ServiceConstant.PLATFORM_SQDM.equals(type)) {// 直通车
			return mSPDMSMSDBBiz;
		} else if (ServiceConstant.PLATFORM_NMG_CHAT.equals(type)) {// 农民
			return mSMSDBBiz;
		} else if (ServiceConstant.PLATFORM_NMG_INVITE.equals(type)) {// 农民
			return mInviteSMSDBBiz;
		} else if (ServiceConstant.PLATFORM_NMG_RESUME.equals(type)) {// 农民
			return mResumeSMSDBBiz;
		} else {
			return null;
		}
	}

}
